package com.itqf.realm;

import com.itqf.bean.DtsAdmin;
import com.itqf.service.DtsAdminService;
import com.itqf.service.DtsPermissionService;
import com.itqf.service.DtsRoleService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/12 17:53
 * description:
 */
public class AuthorizationRealm extends AuthorizingRealm {

    public static final Logger logger = LoggerFactory.getLogger(AuthorizingRealm.class);

    @Autowired
    public DtsAdminService dtsAdminService;
    @Autowired
    public DtsRoleService dtsRoleService;
    @Autowired
    public DtsPermissionService dtsPermissionService;

    /**
     * md5加密的盐
     */
    public static final  String SLAT = "store";

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        //1.获取用户对象
        //获取认证方法放入的对象
        DtsAdmin dtsAdmin = (DtsAdmin) principalCollection.getPrimaryPrincipal();

        if (dtsAdmin == null) {
            logger.warn("[realm授权]用户信息为null!无法查询授权数据!");
            return null;
        }
        //2.查询权限集合

        Set<String> permissionNames = dtsPermissionService.queryPermissionNames(dtsAdmin.getRoleIds());
        //3.查询角色集合
        List<String> roleNames = dtsRoleService.queryRoleNames(dtsAdmin.getRoleIds());
        logger.debug("[realm授权]查询的权限为:{},查询的角色为:{}",permissionNames,roleNames);

        //4.封装数据返回
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addStringPermissions(permissionNames);
        authorizationInfo.addRoles(roleNames);

        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //1.获取账号信息
        String username = (String) authenticationToken.getPrincipal();
        logger.debug("[realm认证] 用户输入的账号为:{}", username);

        if (username == null) {
            logger.warn("[realm认证]用户名不能为null!");
            return null;
        }

        //2.根据账号查询数据库
        DtsAdmin admin = dtsAdminService.queryByUsername(username);
        logger.debug("[realm认证]根据username:{},查询的用户对象为:{}",username,admin);


        //3.判断账号返回结果
        if (admin == null) {
            logger.warn("[realm认证]用户名没有查询到对应的用户数据!");
            return null;
        }
        //4.账号非空封装Info对象 store

        SimpleAuthenticationInfo info =
                new SimpleAuthenticationInfo(admin,admin.getPassword(), ByteSource.Util.bytes(SLAT),"myrealm");

        //5.返回认证数据对象
        return info;
    }
}
